package leetcode.editor.cn;

public class _309_BestTimeToBuyAndSellStockWithCooldown {
    public static void main(String[] args) {
        Solution solution = new _309_BestTimeToBuyAndSellStockWithCooldown().new Solution();
        int[] prices = {1, 2, 3, 0, 2};
        int i = solution.maxProfit(prices);
        System.out.println(i);
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int maxProfit(int[] prices) {
            int n = prices.length;
            if (n == 0 || n == 1) {
                return 0;
            }
            int[][] dp = new int[n][2];
            dp[0][1] = -prices[0];
            dp[0][0] = 0;
            dp[1][1] = Math.max(-prices[0], -prices[1]);
            dp[1][0] = Math.max(0, prices[1] - prices[0]);
            for (int i = 2; i < n; i++) {
                dp[i][1] = Math.max(dp[i - 1][1], dp[i - 2][0] - prices[i]);
                dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
            }
            return dp[n - 1][0];
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}